package 每日一题;

import java.util.Arrays;
/*
关键在为字符串进行排序
 */
public class 最长公共前缀 {
    public static String maxfrontstr(String[] strs) {
      StringBuilder sb=new StringBuilder();
      if(strs==null) return "";
      for (int i=0;i<strs.length;i++){
          if(strs[i].equals("")||strs[i].length()==0){
              return "";
          }
      }
      Arrays.sort(strs);
      int num1=strs[0].length();
      int num2=strs[strs.length-1].length();
      int num=Math.min(num1,num2);
      for (int i=0;i<num;i++){
          if(strs[0].charAt(i)==strs[strs.length-1].charAt(i)){
              sb.append(strs[0].charAt(i));
          }
      }
      return sb.toString();
    }

    public static void main(String[] args) {
        String[] strings={"castomer", "car", "cat" };
        System.out.println(maxfrontstr(strings));
    }
}
